// import router from '@/credit/router'
import { getData } from '@vimi/utils-api'
// import { dateFormat } from '@/common/utils/common'
// import { POSTLOAN_WARNING_DAY } from '@/credit/plugins/constant'

const getFirstData = res => {
  return (res && res.data && res.data.length && res.data[0]) || {}
}

const state = {
  creditData: {
    view: {}, // customer_related_view
    info: {}, // datum_customer_info
  },
  taskCount: {},
  taskCountYd: {},
  taskCountGxs: {},
  taskCountHnd: {},
  taskCountQyd: {},
}

const getters = {
  creditData: state => state.creditData,
  taskCountYd: state => state.taskCountYd,
  taskCountHnd: state => state.taskCountHnd,
  taskCount: state => state.taskCount,
  taskCountGxs: state => state.taskCountGxs,
  taskCountQyd: state => state.taskCountQyd,
}

// this.$store.dispatch('...')
const actions = {
  getTaskCountYd({ commit, rootGetters }) {
    return getData('credit', 'credit_my_task_list_bank28_view', {
      'user_id.eq': rootGetters.userId,
    }).then(res => {
      const data = getFirstData(res)
      commit('taskCountYd', data)
      return data
    })
  },

  async getTaskCountHnd({ commit, rootGetters }) {
    const keys = [
      'normal_total',
      'ex_approval_total',
      'ex_total',
      'normal_funds',
      'ex_approval_funds',
      'ex_funds',
      'no_approval_total',
      'yes_approval_total',
      'update_apply_total',
      'no_approval_funds',
      'yes_approval_funds',
      'update_apply_funds',
      'on_gua_total',
      'expire_gua_total',
      'un_gua_total',
      'ex_gua_total',
      'on_gua_funds',
      'expire_gua_funds',
      'un_gua_funds',
      'ex_gua_funds',
    ]
    const sum = []
    keys.forEach(b => {
      sum.push(`round(sum(${b})|2)`)
    })
    const res = await getData('credit', 'datum_one_county_industry_view_bank', {
      isBIQuery: 1,
      'bank_code.endlike': rootGetters.org.orgCode,
      groupFunc: sum.join(','),
    })
    const data = (res && res.data) || []

    commit('taskCountHnd', data)
    return data
  },

  getTaskCount({ commit, rootGetters }) {
    return getData('credit', 'credit_my_task_list_bank_view', {
      'user_id.eq': rootGetters.userId,
    }).then(res => {
      const data = getFirstData(res)
      commit('taskCount', data)
      return data
    })
  },

  getTaskCountGxs({ commit, rootGetters }) {
    return getData('credit', 'credit_my_task_list_bank_gxs_view', {
      'user_id.eq': rootGetters.userId,
    }).then(res => {
      const data = getFirstData(res)
      commit('taskCountGxs', data)
      return data
    })
  },

  getTaskCountQyd({ commit, rootGetters }) {
    return getData('credit', 'credit_my_task_list_bank_qy_view', {
      'user_id.eq': rootGetters.userId,
    }).then(res => {
      const data = getFirstData(res)
      commit('taskCountQyd', data)
      return data
    })
  },
}

const mutations = {
  creditData: (state, payload) => (state.creditData = payload),
  taskCount: (state, payload) => (state.taskCount = payload),
  taskCountYd: (state, payload) => (state.taskCountYd = payload),
  taskCountHnd: (state, payload) => (state.taskCountHnd = payload),
  taskCountGxs: (state, payload) => (state.taskCountGxs = payload),
  taskCountQyd: (state, payload) => (state.taskCountQyd = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
